#!/usr/bin/python

#------------------------------------------------------------------------------
# $Id$
#
# creator 2derand
#------------------------------------------------------------------------------


def convertFormatedLine(line,format,formatDeffs):
  def cflGet(line,j,out,dump):
    rv = ''
    while line[j]<>out:
      rv += line[j]
      j+=1
      if j==len(line):
        return (rv,j)
      if out==' ':
        if line[j]=='\t' or line[j]==' ':
          out=line[j]
      if len(rv)>=dump[1] and dump[1]<>0 and line[j]<>out:
        print 'asd'
        return (rv,j)
    j+=1
    while line[j]==out:
      j+=1
      if j>=len(line): break
    return (rv,j)
  def cflGetName(dict,name):
    i=0
    rv='%s%d'%(name,i)
    while dict.has_key(rv):
      i+=1
      rv='%s%d'%(name,i)
    return rv
  def cflMagic(dict):
    i=0
    while dict.has_key('year%d'%i):
      if dict.has_key('month%d'%i) and dict.has_key('day%d'%i):
        dt = '%s-%s-%s'%(dict['year%d'%i],dict['month%d'%i],dict['day%d'%i])
        del dict['year%d'%i],dict['month%d'%i],dict['day%d'%i]
        if dict.has_key('hour%d'%i):
          dt += ' %s:'%dict['hour%d'%i]
          del dict['hour%d'%i]
          if dict.has_key('min%d'%i):
            dt += '%s:'%dict['min%d'%i]
            del dict['min%d'%i]
            if dict.has_key('sec%d'%i):
              dt += '%s'%dict['sec%d'%i]
              del dict['sec%d'%i]
            else:
              dt += '00'
          else:
            dt += '00:'
            if dict.has_key('sec%d'%i):
              dt += '%s'%dict['sec%d'%i]
              del dict['sec%d'%i]
            else:
              dt += '00'
        dict[cflGetName(dict,'date')] = dt
      i+=1
    return dict

  i=0
  j=0
  rv = {}
  while i<len(format)-1:
    if formatDeffs.has_key(format[i]):
      (val,j) = cflGet(line,j,format[i+1],formatDeffs[format[i]])
      rv[cflGetName(rv,formatDeffs[format[i]][0])] = val
    i+=1
  if formatDeffs.has_key(format[i]):
    (val,j) = cflGet(line,j,'',formatDeffs[format[i]])
    rv[cflGetName(rv,formatDeffs[format[i]][0])] = val
  return cflMagic(rv)



if (__name__=='__main__'):
  GC_FORMAT_FORMAT = {
                    'order':('Y','M','D','h','m','s','p','o','c','d','l','r','u','t'),
                    'Y':('year',4,'year',),
                    'M':('month',2,'month'),
                    'D':('day',2,'day'),
                    'h':('hour',2,'hour'),
                    'm':('min',2,'min'),
                    's':('sec',2,'sec'),
                    'p':('profit',0,'profit'),
                    'o':('open',0,'open price(points)'),
                    'c':('close',0,'close price(points)'),
                    'd':('dir',1,'direction'),
                    'l':('lots',2,'lots'),
                    'r':('drawdown',0,'drawdown'),
                    'u':('runup',0,'run-up'),
                    't':('activetime',0,'position time'),
                     }
  line = '15.09.2008 09:38 l  5 1.4323  15.09.2008 11:10   1.4276'
  format = 'D.M.Y h:m d l o D.M.Y h:m c'
  print '\033[1;37mline:\033[0m\t%s'%line
  print '\033[1;37mformat:\033[0m\t%s'%format
  print '\033[1;37mresult:\033[0m',convertFormatedLine(line,format,GC_FORMAT_FORMAT)
